package com.ruoyi.rsikManage.service.Impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.rsikManage.domain.VideoResourceTreeNode;
import com.ruoyi.rsikManage.mapper.VideoResourceTreeMapper;
import com.ruoyi.rsikManage.service.VideoResourceTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoResourceTreeServiceImpl implements VideoResourceTreeService {
    @Autowired
    VideoResourceTreeMapper videoResourceTreeMapper;

    @Override
    public List<VideoResourceTreeNode> selectTreeByTreeType(String treeType) {
        return videoResourceTreeMapper.selectTreeByTreeType(treeType);
    }

    @Override
    public List<VideoResourceTreeNode> selectAllTreeRoot() {
        return videoResourceTreeMapper.selectAllTreeRoot();
    }

    @Override
    public VideoResourceTreeNode selectTreeNodeById(Integer nodeId) {

        return videoResourceTreeMapper.selectTreeNodeById(nodeId);
    }

    @Override
    public boolean checkTreeNodeNameUnique(VideoResourceTreeNode treeNode) {
//        Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
        Integer nodeId = StringUtils.isNull(treeNode.getNodeId()) ? -1 : treeNode.getNodeId();
        VideoResourceTreeNode info = videoResourceTreeMapper.checkTreeNodeNameUnique(treeNode.getNodeName(), treeNode.getParentId());
        if (StringUtils.isNotNull(info) && info.getNodeId().intValue() != nodeId.intValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertTreeNode(VideoResourceTreeNode treeNode) {
        VideoResourceTreeNode parentNode = videoResourceTreeMapper.selectTreeNodeById(treeNode.getParentId());
        if (parentNode != null) {
            treeNode.setAncestors(parentNode.getAncestors() + "," + parentNode.getNodeId());
        }else {
            treeNode.setAncestors("0");
        }
        return videoResourceTreeMapper.insertTreeNode(treeNode);
    }

    @Override
    public List<String> selectAllTreeType() {
        return videoResourceTreeMapper.selectAllTreeType();
    }

    @Override
    public int updateTreeNode(VideoResourceTreeNode treeNode) {
        return videoResourceTreeMapper.updateTreeNode(treeNode);
    }


    @Override
    public int deleteTreeNodeByNodeId(Integer nodeId) {
        return videoResourceTreeMapper.deleteTreeNodeByNodeId(nodeId);
    }
}
